// pages/ucenter/charge/index.js
// pages/ucenter/personal/index.js
var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');
var user = require('../../../utils/user.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {
      nickName: '',
      avatarUrl: '',
      avatar: '',
      realName: '',
      mobile: '',
    },
    config: {
      vip_charge_money: 0,
      vip_charge_month: 0,
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getUserInfo();
    this.getConfig();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  getUserInfo() {
    let that = this;
    wx.showLoading({
      title: '加载中...',
    });

    util.request(api.userInfoUrl, 'GET').then(function (res) {
      if (res.errno === 0) {
        let data = res.data;
        that.setData({
          userInfo: data,
        })

      }
      wx.hideLoading();
    }).catch(err => {
      wx.hideLoading();
    });
  },

  getConfig() {
    console.log("config");
    try {
      let that = this;
      util.request(api.config, 'GET').then(function (res) {
        if (res.errno === 0) {
          let data = res.data;
          that.setData({
            config: data,
          })

        }

      }).catch(err => {

      });
    } catch (err) {
      console.error(err);
    }

  },

  createOrder() {
    let that = this;
    wx.showLoading({
      title: '加载中...',
    });

    util.request(api.vipOrder, 'GET').then(function (res) {
      if (res.errno === 0) {
        let orderNo = res.data;
        that.orderPay(orderNo);
      } else {
        wx.hideLoading();
      }
    }).catch(err => {
      wx.hideLoading();
      util.showErrorToast(err.errmsg)

    });
  },

  orderPay(orderNo) {
    util.request(api.wxPay, {
        orderNo: orderNo,
      },
      'GET').then(function (res) {
      if (res.errno === 0) {
        const payParam = res.data;
        console.log("支付过程开始" + JSON.stringify(payParam));
        wx.requestPayment({
          'timeStamp': payParam.timeStamp,
          'nonceStr': payParam.nonceStr,
          'package': payParam.packageValue,
          'signType': payParam.signType,
          'paySign': payParam.paySign,
          'success': function (res) {
            console.log("支付过程成功");
            wx.redirectTo({
              url: '/pages/payResult/index?status=1&orderNo=' + orderNo
            });

          },
          'fail': function (res) {
            console.log("支付过程失败");
            wx.redirectTo({
              url: '/pages/payResult/index?status=0&orderNo=' + orderNo
            });
          },
          'complete': function (res) {
            console.log("支付过程结束")
          }
        });
      }
      wx.hideLoading();
    }).catch(err => {
      wx.hideLoading();
      util.showErrorToast(err.errmsg)
    });
  }
})